home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / src / ucs2.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  1.3 KB  |  48 lines

  1. /*
  2.  * UCS-2
  3.  */
  4.  
  5. /* Here we accept FFFE/FEFF marks as endianness indicators everywhere
  6.    in the stream, not just at the beginning. The default is big-endian. */
  7. /* The state is 0 if big-endian, 1 if little-endian. */
  8. static int
  9. ucs2_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  10. {
  11.   state_t state = conv->istate;
  12.   int count = 0;
  13.   for (; n >= 2;) {
  14.     wchar_t wc = (state ? s[0] + (s[1] << 8) : (s[0] << 8) + s[1]);
  15.     s += 2; n -= 2; count += 2;
  16.     if (wc == 0xfeff) {
  17.     } else if (wc == 0xfffe) {
  18.       state ^= 1;
  19.     } else if (wc >= 0xd800 && wc < 0xe000) {
  20.       return RET_ILSEQ;
  21.     } else {
  22.       *pwc = wc;
  23.       conv->istate = state;
  24.       return count;
  25.     }
  26.   }
  27.   conv->istate = state;
  28.   return RET_TOOFEW(count);
  29. }
  30.  
  31. /* But we output UCS-2 in big-endian order, without byte-order mark. */
  32. /* RFC 2152 says:
  33.    "ISO/IEC 10646-1:1993(E) specifies that when characters the UCS-2 form are
  34.     serialized as octets, that the most significant octet appear first." */
  35. static int
  36. ucs2_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  37. {
  38.   if (wc < 0x10000 && wc != 0xfffe && !(wc >= 0xd800 && wc < 0xe000)) {
  39.     if (n >= 2) {
  40.       r[0] = (unsigned char) (wc >> 8);
  41.       r[1] = (unsigned char) wc;
  42.       return 2;
  43.     } else
  44.       return RET_TOOSMALL;
  45.   } else
  46.     return RET_ILSEQ;
  47. }
  48.